﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;

namespace WeForms
{
    public class ObservableCollection<T> : Collection<T>
{

     public event CollectionChangedEventHandler CollectionChanged;
   

    protected override void ClearItems()
    {
        base.ClearItems();
        this.OnCollectionChanged(new CollectionChangedEventArgs(ChangeType.Cleared));
    }

    protected override void InsertItem(int index, T item)
    {
        base.InsertItem(index, item);
        this.OnCollectionChanged(new CollectionChangedEventArgs(ChangeType.Added));
    }

    private void OnCollectionChanged(CollectionChangedEventArgs e)
    {
        if (this.CollectionChanged != null)
        {
            this.CollectionChanged(this, e);
        }
    }

    protected override void RemoveItem(int index)
    {
        base.RemoveItem(index);
        this.OnCollectionChanged(new CollectionChangedEventArgs(ChangeType.Removed));
    }

    protected override void SetItem(int index, T item)
    {
        base.SetItem(index, item);
        this.OnCollectionChanged(new CollectionChangedEventArgs(ChangeType.Changed));
    }
}

    public delegate void CollectionChangedEventHandler(object sender, CollectionChangedEventArgs e);




    public class CollectionChangedEventArgs : EventArgs
    {
        public CollectionChangedEventArgs(ChangeType type)
        {
            this.Type = type;
        }

        public ChangeType Type { get; private  set; }
    }

    public enum ChangeType
    {
        Added,
        Removed,
        Changed,
        Cleared
    }
 


}
